import os
from common.base import *


class MouseOperation:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://sahitest.com/demo/clicks.htm')

    # 单击
    def click(self):
        btn = self.driver.find_element(By.XPATH, '/html/body/form/input[3]')
        # 创建AC对象,参数是driver对象
        ac = ActionChains(self.driver)
        # 绑定操作的元素后用perform执行
        ac.click(btn).perform()

    # 右击
    def context_click(self):
        ac = ActionChains(self.driver)
        btn = self.driver.find_element(By.XPATH, '/html/body/form/input[4]')
        ac.context_click(btn).perform()

    # 双击
    def double_click(self):
        ac = ActionChains(self.driver)
        btn = self.driver.find_element(By.XPATH, '/html/body/form/input[2]')
        ac.double_click(btn).perform()

    # 拖动
    def drag_and_drop(self):
        self.driver.get('https://sahitest.com/demo/dragDropMooTools.htm')
        ac = ActionChains(self.driver)
        source = self.driver.find_element(By.ID, 'dragger')
        target = self.driver.find_element(By.XPATH, '//div[4]')
        ac.drag_and_drop(source, target).perform()

    # 悬停
    def mouseover(self):
        self.driver.get('https://sahitest.com/demo/mouseover.htm')
        ac = ActionChains(self.driver)
        btn = self.driver.find_element(By.NAME, 'b1')
        ac.move_to_element(btn).perform()

    # 按下不动
    def click_and_hold(self):
        # TODO 请求本地地址
        # url = 'file:///D:/ITsource%E5%AD%A6%E4%B9%A0%E6%97%A5%E5%BF%97/0512selenium%E8%AF%BE%E4%BB%B6/teacher_code/automated_testing/day3/html/mouse_hold.html'

        # TODO 找到html的绝对路径os.path.abspath('html')
        # url = 'file:///' + os.path.abspath('html') + '/mouse_hold.html'

        # TODO 找到当前文件所在目录的绝对路径os.path.dirname(__file__)
        url = 'file:///' + os.path.dirname(__file__) + '/html/mouse_hold.html'

        self.driver.get(url)
        ac = ActionChains(self.driver)
        btn = self.driver.find_element(By.ID, 'btn1')
        ac.click_and_hold(btn).perform()

    def quit(self):
        sleep(3)
        self.driver.quit()


if __name__ == '__main__':
    mouse = MouseOperation()
    # mouse.click()
    # mouse.context_click()
    # mouse.double_click()
    # mouse.drag_and_drop()
    # mouse.mouseover()
    mouse.click_and_hold()
    mouse.quit()

